/*
 * Simple Test suite for Rainbowcrack and its derivatives
 * 
 * A successful run will exit with EXIT_SUCCESS which can be detected from shell
 * scripts.  An error at any stage will produce debug on STDOUT and exit with
 * EXIT_FAILURE.
 * 
 * To build:
 *   g++ HashAlgorithm.cpp md4.cpp md5.cpp rtest.cpp -lssl -o rtest
 * 
 * 
 * Copyright (c) 2009 MC <m@cotdp.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "HashAlgorithm.h"

#include "Public.h"

#include <string.h>
#include <stdlib.h>


// 
typedef void (*HASHROUTINE)(unsigned char* pPlain, int nPlainLen, unsigned char* pHash);

//
typedef struct TestVector_s {
    const char *input;
    unsigned char digest[MAX_HASH_LEN];
} TestVector;


/**
 */
void HexDump(unsigned char* digest, int len )
{
  for (int i = 0; i < len; i++)
    printf ("%02x", digest[i]);
}


/**
 */
int TestHashAlgorithm( const char* label, HASHROUTINE pHashRoutine, unsigned char* pPlain, unsigned char* pHashExpected, int nHashLength )
{
    unsigned char szHash[MAX_HASH_LEN];
    int nPlainLength = strlen( (const char*) pPlain );
    pHashRoutine( pPlain, nPlainLength, szHash );
    int result = memcmp( pHashExpected, szHash, nHashLength );
    if ( result != 0 ) {
        printf( "%s test vector '%s' failed\n", label, pPlain );
        printf( "  expected: " ); HexDump( pHashExpected, nHashLength ); printf("\n");
        printf( "  got:      " ); HexDump( szHash, nHashLength ); printf("\n");
    }
    return result;
}


/**
 */
int main(int argc, char* argv[])
{
    int result = EXIT_SUCCESS;
    
    // MD4 test vectors, from www.ietf.org/rfc/rfc1320.txt
    TestVector MD4_test_vectors[] = {
        { "", {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} },
        { "a", {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} },
        { "abc", {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} },
        { "message digest", {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} },
        { "abcdefghijklmnopqrstuvwxyz", {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} },
        { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} },
        { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} }
    };
    for(int i = 0; i < 7; i++) {
        if ( TestHashAlgorithm( "MD4", HashMD4, (unsigned char*)MD4_test_vectors[i].input, MD4_test_vectors[i].digest, 16 ) ) {
            result = EXIT_FAILURE;
        }
    }

    // MD5 test vectors, from www.ietf.org/rfc/rfc1321.txt
    TestVector MD5_test_vectors[] = {
        { "", {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e} },
        { "a", {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61} },
        { "abc", {0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72} },
        { "message digest", {0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0} },
        { "abcdefghijklmnopqrstuvwxyz", {0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b} },
        { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", {0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f} },
        { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", {0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a} }
    };
    for(int i = 0; i < 7; i++) {
        if ( TestHashAlgorithm( "MD5", HashMD5, (unsigned char*)MD5_test_vectors[i].input, MD5_test_vectors[i].digest, 16 ) ) {
            result = EXIT_FAILURE;
        }
    }
    
    // LM test vectors, created using Crypt::SmbHash
    TestVector LM_test_vectors[] = {
        { "", {0xaa, 0xd3, 0xb4, 0x35, 0xb5, 0x14, 0x04, 0xee} },
        { "a", {0x75, 0x84, 0x24, 0x8b, 0x8d, 0x2c, 0x9f, 0x9e} },
        { "abc", {0x8c, 0x6f, 0x5d, 0x02, 0xde, 0xb2, 0x15, 0x01} },
        { "message digest", {0x37, 0x55, 0x65, 0x57, 0xe6, 0xc7, 0x97, 0x6d} },
        { "abcdefghijklmnopqrstuvwxyz", {0xe0, 0xc5, 0x10, 0x19, 0x9c, 0xc6, 0x6a, 0xbd} },
        { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", {0xe0, 0xc5, 0x10, 0x19, 0x9c, 0xc6, 0x6a, 0xbd} },
        { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", {0x01, 0x82, 0xbd, 0x0b, 0xd4, 0x44, 0x4b, 0xf8} }
    };
    for(int i = 0; i < 7; i++) {
        if ( TestHashAlgorithm( "LM", HashLM, (unsigned char*)LM_test_vectors[i].input, LM_test_vectors[i].digest, 8 ) ) {
            result = EXIT_FAILURE;
        }
    }
    
    // NTLM test vectors, created using Crypt::SmbHash
    TestVector NTLM_test_vectors[] = {
        { "", {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} },
        { "a", {0x18, 0x6c, 0xb0, 0x91, 0x81, 0xe2, 0xc2, 0xec, 0xaa, 0xc7, 0x68, 0xc4, 0x7c, 0x72, 0x99, 0x04} },
        { "abc", {0xe0, 0xfb, 0xa3, 0x82, 0x68, 0xd0, 0xec, 0x66, 0xef, 0x1c, 0xb4, 0x52, 0xd5, 0x88, 0x5e, 0x53} },
        { "message digest", {0x94, 0xa8, 0xa6, 0xcc, 0x36, 0x10, 0x8b, 0x93, 0xdb, 0x33, 0x0d, 0xe5, 0x4b, 0x90, 0xbd, 0x4b} },
        { "abcdefghijklmnopqrstuvwxyz", {0x0b, 0xd6, 0x31, 0x85, 0xf3, 0x48, 0x4b, 0xb0, 0x00, 0x28, 0x6c, 0x85, 0x91, 0x7d, 0xc1, 0x2e} },
        { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", {0x2e, 0x74, 0xcc, 0x46, 0xc9, 0x6e, 0xe4, 0xca, 0xee, 0x5d, 0xf2, 0x0d, 0x08, 0x98, 0xfe, 0xf8} },
        { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", {0xcf, 0x17, 0xb1, 0xae, 0x26, 0x06, 0xaf, 0xa9, 0x64, 0x19, 0x36, 0x90, 0xdf, 0x75, 0x43, 0xb1} }
    };
    for(int i = 0; i < 7; i++) {
        if ( TestHashAlgorithm( "NTLM", HashNTLM, (unsigned char*)NTLM_test_vectors[i].input, NTLM_test_vectors[i].digest, 16 ) ) {
            result = EXIT_FAILURE;
        }
    }

    return result;
}
